﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Commands;
using ModulesInfrastructure.ViewModels;
using EventInfrastructure;
using ServerConnection.ShopServiceReference;
using ModuleCategoryAdd.Views;
using ShellInfrastructure;
using ServerConnection;

namespace ModuleCategoryAdd.ViewModels
{
    public class CategoryAddViewModel : SharedViewModel, ICategoryAddViewModel
    {  

        #region private filds

        private IEventAggregator m_eventAggregator;

        private ICommand m_OkCategoryCommand;
        private ICommand m_CancelCategoryCommand;
        private ICommand m_AddLocalizationCommand;
        private ICommand m_DeleteLocalizationCommand;

        private ProductLocalizationModel m_selectedLocalization;
        private bool m_flagAdd;

        #endregion
       
        #region public proprties

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        public CategoryAll ShowCategory { get; private set; }
        public ICommand AddLocalizationCommand
        {
            get { return m_AddLocalizationCommand; }
            set
            {
                m_AddLocalizationCommand = value;
                RaisePropertyChanged(() => AddLocalizationCommand);
            }
        }
        public ICommand DeleteLocalizationCommand
        {
            get { return m_DeleteLocalizationCommand; }
            set
            {
                m_DeleteLocalizationCommand = value;
                RaisePropertyChanged(() => DeleteLocalizationCommand);

            }
        }
        public ICommand OkCategoryCommand
        {
            get { return m_OkCategoryCommand; }
            set
            {
                m_OkCategoryCommand = value;
                RaisePropertyChanged(() => OkCategoryCommand);
            }
        }
        public ICommand CancelCategoryCommand
        {
            get { return m_CancelCategoryCommand; }
            set
            {
                m_CancelCategoryCommand = value;
                RaisePropertyChanged(() => CancelCategoryCommand);
            }
        }

        public ProductLocalizationModel SelectedLocalization
        {
            get { return m_selectedLocalization; }
            set
            {
                m_selectedLocalization = value;
                RaisePropertyChanged(() => this.m_selectedLocalization);
                RaisePropertyChanged(() => this.SelectedLocalization);
            }

        }

        #endregion

        #region constructor

        public CategoryAddViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator =eventAggregator;
            eventAggregator.GetEvent<AddCategoryEvent>().Subscribe(onAddCategoryEvent);
            eventAggregator.GetEvent<CategoryEditEvent>().Subscribe(onCategoryEditEvent);
            eventAggregator.GetEvent<AddSubCategoryEvent>().Subscribe(onAddSubCategoryEvent);
            eventAggregator.GetEvent<SubCategoryEditEvent>().Subscribe(onSubCategoryEditEvent);
            m_OkCategoryCommand = new DelegateCommand(onOkCategoryCommand);
            m_CancelCategoryCommand = new DelegateCommand(onCancelCategoryCommand);
            m_AddLocalizationCommand = new DelegateCommand(onAddLocalizationCommand);
            m_DeleteLocalizationCommand = new DelegateCommand(onDeleteLocalizationCommand);
        }

        #endregion

        #region private function

        private void onAddCategoryEvent(bool b)
        {
            m_flagAdd = true;
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<ICategoryAddView>());
            this.ShowCategory = new CategoryAll();
            this.ShowCategory.ParentId = 0;
            RaisePropertyChanged(() => this.ShowCategory);
        }

        private void onCategoryEditEvent(int categoryId)
        {
            m_flagAdd = false;
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<ICategoryAddView>());
            this.ShowCategory = Service.Instance.GetCategoryAllById(categoryId);
            RaisePropertyChanged(() => this.ShowCategory);
        }

        private void onAddSubCategoryEvent(int parentId)
        {
            m_flagAdd = true;
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<ICategoryAddView>());
            this.ShowCategory = new CategoryAll();
            this.ShowCategory.ParentId = parentId;
            RaisePropertyChanged(() => this.ShowCategory);
        }

        private void onSubCategoryEditEvent(int subCategoryId)
        {
            m_flagAdd = false;
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<ICategoryAddView>());
            this.ShowCategory = Service.Instance.GetCategoryAllById(subCategoryId);
            RaisePropertyChanged(() => this.ShowCategory);
        }

        private void onOkCategoryCommand()
        {
            if (m_flagAdd.Equals(true))
            {
                Service.Instance.AddCtegory(ShowCategory);
                m_flagAdd = false;
            }
            else
                Service.Instance.UpdateCtegory(ShowCategory);

           m_eventAggregator.GetEvent<CategorySaveEvent>().Publish(true);
        }

        private void onCancelCategoryCommand()
        {
            m_eventAggregator.GetEvent<ShowProductEvent>().Publish(true);
            m_eventAggregator.GetEvent<SelectCategoryEvent>().Publish(true);
        }

        private void onAddLocalizationCommand()
        {
            if (ShowCategory.Localizations != null)
            {
                CategoryLocalizationModel[] pArray = ShowCategory.Localizations;
                this.ShowCategory.Localizations = new CategoryLocalizationModel[pArray.Length + 1];
                for (int i = 0; i < pArray.Length; i++)
                {
                    this.ShowCategory.Localizations[i] = pArray[i];
                }
                this.ShowCategory.Localizations[pArray.Length] = new CategoryLocalizationModel();
            }
            else
            {
                this.ShowCategory.Localizations = new CategoryLocalizationModel[1];
                this.ShowCategory.Localizations[0] = new CategoryLocalizationModel();
            }
            RaisePropertyChanged(() => this.ShowCategory.Localizations);
            RaisePropertyChanged(() => this.ShowCategory);

        }

        private void onDeleteLocalizationCommand()
        {
            CategoryLocalizationModel[] pArray = ShowCategory.Localizations;
            this.ShowCategory.Localizations = new CategoryLocalizationModel[pArray.Length - 1];
            for (int i = 0; i < this.ShowCategory.Localizations.Length; i++)
            {
                if (!pArray[i].Equals(m_selectedLocalization))
                    this.ShowCategory.Localizations[i] = pArray[i];
            }
            RaisePropertyChanged(() => this.ShowCategory);
        }

        #endregion

    }
}
